//
//  LNMainController.m
//  LNBeeHive
//
//  Created by wwww on 2019/9/27.
//  Copyright © 2019 淘发现1iOS. All rights reserved.
//

#import "LNMainController.h"
#import "LNMainViewModel.h"
#import "LNService.h"
#import "UIImage+YYAdd.h"
#import "UIColor+YYAdd.h"
#import "BHTimeProfiler.h"
@interface LNMainController ()

@property (nonatomic,strong,readwrite) LNMainViewModel *viewModel;

@property (nonatomic, readwrite, strong) UITabBarController *tabBarController;

@property(nonatomic,strong) NSMutableArray *registerViewControllers;
@end

@implementation LNMainController
@dynamic viewModel;

// 第一次使用这个类调用一次
+ (void)initialize{
    // 2.设置UINavigationBar的主题
    [self _setupNavigationBarTheme];
    
    // 3.设置UIBarButtonItem的主题
    [self _setupBarButtonItemTheme];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [[BHTimeProfiler sharedTimeProfiler] printOutTimeProfileResult];
    [self setNeedsStatusBarAppearanceUpdate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置界面
    [self setupUI];
    
    self.registerViewControllers = [[NSMutableArray alloc] initWithCapacity:1];

    id<LNHomeServiceProtocol> v1 = [[BeeHive shareInstance] createService:@protocol(LNHomeServiceProtocol)];
    UIViewController *vc1 = [v1 LNViewController];
    [self registerViewController:vc1 title:@"首页" iconName:nil];
    
    id<LNShopCartServiceProtocol> v2 = [[BeeHive shareInstance] createService:@protocol(LNShopCartServiceProtocol)];
    UIViewController *vc2 = [v2 LNViewController];
    [self registerViewController:vc2 title:@"购物车" iconName:nil];
    
    id<LNMineServiceProtocol> v3 = [[BeeHive shareInstance] createService:@protocol(LNMineServiceProtocol)];
    UIViewController *vc3 = [v3 LNViewController];
    [self registerViewController:vc3 title:@"我的" iconName:nil];
    
    UINavigationController *nav = [self.registerViewControllers firstObject];
    /// 配置栈底
    [LNSharedAppDelegate.navigationControllerStack pushNavigationController:nav];
}

-(void)registerViewController:(UIViewController *)vc title:(NSString *)title iconName:(NSString *)iconName
{
    //添加导航控制器
    UINavigationController *nav = [UINavigationController rootVC:vc transitionScale:YES];
    nav.gk_openScrollLeftPush = YES;
    nav.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"Home.bundle/%@", iconName]];
    nav.tabBarItem.title = title;
    NSLog(@"%@  ---- %@",title,nav);
    [self.registerViewControllers addObject:nav];
    self.tabBarController.viewControllers = self.registerViewControllers;
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    UINavigationController *nav = (UINavigationController *)viewController;
    [nav popToRootViewControllerAnimated:NO];
    
    [LNSharedAppDelegate.navigationControllerStack popNavigationController];
    [LNSharedAppDelegate.navigationControllerStack pushNavigationController:nav];
}


#pragma makr - 设置界面
-(void)setupUI{
    
    self.tabBarController = [[UITabBarController alloc] init];
    /// 添加子控制器
    [self.view addSubview:self.tabBarController.view];
    [self addChildViewController:self.tabBarController];
    self.tabBarController.delegate = self;
//    // kvc替换系统的tabBar
//    SKTabBar *tabbar = [[SKTabBar alloc] init];
//    tabbar.tabBarDelegate = self;
//    //kvc实质是修改了系统的_tabBar
//    [self.tabBarController setValue:tabbar forKeyPath:@"tabBar"];
//
    // 统一设置导航栏样式
    GKNavigationBarConfigure *configure = [GKNavigationBarConfigure sharedInstance];
    [configure setupDefaultConfigure];

    // 设置自定义样式
    configure.backgroundColor = [UIColor colorWithRed:(212 / 255.0) green:(25 /255.0) blue:(37 / 255.0) alpha:1.0];
    configure.titleColor = [UIColor whiteColor];
    configure.titleFont  = [UIFont systemFontOfSize:18];
    configure.statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark - Ovveride
- (BOOL)shouldAutorotate {
    return self.tabBarController.selectedViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.tabBarController.selectedViewController.supportedInterfaceOrientations;
}

- (BOOL)prefersStatusBarHidden{
    return self.tabBarController.selectedViewController.prefersStatusBarHidden;
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    UINavigationController *nav = self.tabBarController.selectedViewController;
    NSLog(@"aa==%@  bb==%d",nav.visibleViewController,nav.visibleViewController.gk_statusBarStyle);
    return nav.visibleViewController.gk_statusBarStyle;
}

/**
 *  设置UINavigationBarTheme的主题
 */
+ (void) _setupNavigationBarTheme{
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    /// 设置背景
    //!!!: 必须设置为透明  不然布局有问题 ios8以下  会崩溃/ 如果iOS8以下  请再_setup里面 设置透明 self.navigationBar.translucent = YES;
    //    [appearance setTranslucent:YES]; /// 必须设置YES
    
    // 设置导航栏的样式
    [appearance setBarStyle:UIBarStyleDefault];
    //设置导航栏文字按钮的渲染色
    [appearance setTintColor:[UIColor blackColor]];
    // 设置导航栏的背景渲染色
    //CGFloat rgb = 0.1;
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:18.0f];
    textAttrs[NSForegroundColorAttributeName] =[UIColor blackColor];

    // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset =  CGSizeZero;
    textAttrs[NSShadowAttributeName] = shadow;
    [appearance setTitleTextAttributes:textAttrs];
    
    /// 去掉导航栏的阴影图片
    [appearance setShadowImage:[UIImage new]];
//    // 导航栏背景颜色
    [appearance setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"FF4C00"] size:CGSizeMake([[UIScreen mainScreen] bounds].size.width, 64)] forBarMetrics:UIBarMetricsDefault];
    
}

/**
 *  设置UIBarButtonItem的主题
 */
+ (void)_setupBarButtonItemTheme{
    // 通过appearance对象能修改整个项目中所有UIBarButtonItem的样式
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    
    CGFloat fontSize = 16.0f;
    
    /**设置文字属性**/
    // 设置普通状态的文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"333333"];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:fontSize];
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset =  CGSizeZero;
    textAttrs[NSShadowAttributeName] = shadow;
    [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    
    // 设置高亮状态的文字属性
    NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
    highTextAttrs[NSForegroundColorAttributeName] =[UIColor blackColor];
    [appearance setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
    
    // 设置不可用状态(disable)的文字属性
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
    disableTextAttrs[NSForegroundColorAttributeName] = [[UIColor whiteColor] colorWithAlphaComponent:.5f];
    [appearance setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
}

@end
